package com.frxuexi.mine.view

import android.widget.Space
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.OutlinedCard
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import coil3.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.R
import com.frxuexi.lib_base.RoutePath
import com.tencent.mmkv.MMKV

@Composable
fun MinePage(navHostController: NavHostController) {
    val kv = MMKV.defaultMMKV()
    var uname = kv.decodeString("uname", "未登录")!!
    var isShow by remember {
        mutableStateOf(false)
    }
    Column(
        Modifier
            .fillMaxSize()
            .padding(20.dp),
        verticalArrangement = Arrangement.spacedBy(10.dp)
    ) {
        Text(text = "我的", fontSize = 28.sp, fontWeight = FontWeight.Bold)
        ElevatedCard(colors= CardDefaults.cardColors(containerColor = Color.White)) {
            Row(modifier = Modifier
                .fillMaxWidth()
                .padding(20.dp, 10.dp)
                .clickable {
                    if (uname == "未登录")
                        navHostController.navigate(RoutePath.LOGIN)
                    else {
                        isShow = true
                    }
                },verticalAlignment = Alignment.CenterVertically) {
                AsyncImage(
                    model = "https://img1.baidu.com/it/u=4035546522,389745751&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1735146000&t=a6cb452f25f5456854d0cc65889449b6",
                    contentDescription = "",
                    modifier = Modifier
                        .size(80.dp)
                        .clip(CircleShape),
                    contentScale = ContentScale.Crop
                )
                Spacer(modifier = Modifier.width(10.dp))
                Text(text = uname)
            }
        }

        Text(text = "常用功能")
        ElevatedCard(colors= CardDefaults.cardColors(containerColor = Color.White)) {
            Row(modifier = Modifier
                .fillMaxWidth()
                .padding(20.dp, 10.dp)) {
                Column(Modifier.clickable {
                    if (uname == "未登录"){
                        ToastUtils.showLong("还未登录")
                    }else{
                        navHostController.navigate(RoutePath.LEAVE_LIST)
                    }
                }) {
                    Image(painter = painterResource(id = R.drawable.icon359), contentDescription = "",Modifier.size(30.dp))
                    Spacer(modifier = Modifier.height(8.dp))
                    Text(text = "请假")
                }
                Spacer(modifier = Modifier.width(20.dp))
                Column (Modifier.clickable {
                    if (uname == "未登录"){
                        ToastUtils.showLong("还未登录")
                    }else{
                        navHostController.navigate(RoutePath.APPROVE_LEAVE_LIST)
                    }
                }){
                    Image(painter = painterResource(id = R.drawable.icon81), contentDescription = "",Modifier.size(30.dp))
                    Spacer(modifier = Modifier.height(8.dp))
                    Text(text = "审批")
                }
            }
        }
    }

    if(isShow){
        AlertDialog(onDismissRequest = { isShow=false }, confirmButton = {
            Text(text = "确认",Modifier.clickable {
                kv.remove("uname")
                kv.remove("token")
                uname = "未登录"
                isShow=false
            })
        }, title = {
            Text(text = "退出提醒")
        }, text = {
            Text(text = "是否要退出当前登录？")
        })
    }
}